package com.ruoyi.control.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.dto.AircompressorHistoryDto;
import com.ruoyi.control.dto.GasDrainagePumpRoomHistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.AircompressorHistoryMapper;
import com.ruoyi.control.domain.AircompressorHistory;
import com.ruoyi.control.service.IAircompressorHistoryService;

/**
 * 空压机历史数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class AircompressorHistoryServiceImpl extends ServiceImpl<AircompressorHistoryMapper, AircompressorHistory> implements IAircompressorHistoryService //修改
{
    @Autowired
    private AircompressorHistoryMapper aircompressorHistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询空压机历史数据
     *
     * @param id 空压机历史数据主键
     * @return 空压机历史数据
     */
    @Override
    public AircompressorHistory selectAircompressorHistoryById(String id)
    {
        return aircompressorHistoryMapper.selectAircompressorHistoryById(id);
    }

    /**
     * 查询空压机历史数据列表
     *
     * @param aircompressorHistory 空压机历史数据
     * @return 空压机历史数据
     */
    @Override
    public List<AircompressorHistory> selectAircompressorHistoryList(AircompressorHistory aircompressorHistory)
    {
        return aircompressorHistoryMapper.selectAircompressorHistoryList(aircompressorHistory);
    }

    /**
     * 新增空压机历史数据
     *
     * @param aircompressorHistory 空压机历史数据
     * @return 结果
     */
    @Override
    public int insertAircompressorHistory(AircompressorHistory aircompressorHistory)
    {
        return aircompressorHistoryMapper.insertAircompressorHistory(aircompressorHistory);
    }

    /**
     * 修改空压机历史数据
     *
     * @param aircompressorHistory 空压机历史数据
     * @return 结果
     */
    @Override
    public int updateAircompressorHistory(AircompressorHistory aircompressorHistory)
    {
        aircompressorHistory.setUpdateTime(DateUtils.getNowDate());
        return aircompressorHistoryMapper.updateAircompressorHistory(aircompressorHistory);
    }

    /**
     * 批量删除空压机历史数据
     *
     * @param ids 需要删除的空压机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteAircompressorHistoryByIds(String[] ids)
    {
        return aircompressorHistoryMapper.deleteAircompressorHistoryByIds(ids);
    }

    /**
     * 删除空压机历史数据信息
     *
     * @param id 空压机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteAircompressorHistoryById(String id)
    {
        return aircompressorHistoryMapper.deleteAircompressorHistoryById(id);
    }

    @Override
    public Integer[] selectByTagName(String tagName) {
        Integer[] history= new Integer[60*24];
        List<AircompressorHistory> historyList=aircompressorHistoryMapper.selectByTagName(tagName);
        for (int i=0; i<60*24;i++){
            if (i>=historyList.size()){
                history[i]= 0;
            }else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }


    /**
     * 空压机7天故障
     * @return
     */
    public List<AircompressorHistoryDto> getAircompressorTotalFault() {

        final String key = "kyjGzKey";
        List<AircompressorHistoryDto> cachedValueList = redisCache.getCacheObject(key);

        List<AircompressorHistoryDto> list = aircompressorHistoryMapper.getAircompressorTotalFault();


        if (list == null || list.isEmpty()) {
            //list  为空
            List<AircompressorHistoryDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                AircompressorHistoryDto abnormalCountDto = new AircompressorHistoryDto();
                abnormalCountDto.setCount("0");
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = objInList.getCount();
                return abnormalCount;
            }).collect(Collectors.toList());
            redisCache.setCacheObject(key, collect);
        } else {
            List<AircompressorHistoryDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                AircompressorHistoryDto abnormalCountDto = new AircompressorHistoryDto();
                abnormalCountDto.setCount("0");
                if (abnormalCountDto.getUpdateDate() != null) {
                    abnormalCountDto.setUpdateDate(abnormalCountDto.getUpdateDate());
                }
                resultList.add(abnormalCountDto);
            }

            redisCache.setCacheObject(key, resultList);

        }

        return cachedValueList;

    }

    public List<AircompressorHistoryDto> getAircompressorPowerValueList(String str) {
        String[] keys = {"kyjGhKey1", "kyjGhKey2", "kyjGhKey3"};
        String[] prefixes = {"kongya_j/kyj1.", "kongya_j/kyj2.", "kongya_j/kyj3."};

        int index = Integer.parseInt(str) - 1;
        String key = keys[index];
        String dy = prefixes[index] + "dianya1";
        String dl = prefixes[index] + "dianliu1";

        List<AircompressorHistoryDto> cachedValueList = redisCache.getCacheObject(key);
        if (cachedValueList != null) {
            return cachedValueList;
        }

        List<AircompressorHistoryDto> list = aircompressorHistoryMapper.getAircompressorPowerValueList(dy, dl);
        List<AircompressorHistoryDto> processedList = list.stream().map(objInList -> {
            Double avgPower = objInList.getAvgPower();
            Double consumptionValue = avgPower * 60 * 60;
            objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", consumptionValue)));
            return objInList;
        }).collect(Collectors.toList());

        redisCache.setCacheObject(key, processedList);
        return processedList;
    }
}
